package com.interactive1.dbutils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;

import com.interactive1.utils.Functions;



// Referenced classes of package com.interactive1.dbutils:
//            ConnectionPool, Result

public class DBUtil
{

    public DBUtil()
    {
    }

    public static int execute(String s)
        throws Exception
    {
        return update("DEFAULT", s);
    }

    public static int execute(String s, String s1)
        throws Exception
    {
        return update(s, s1);
    }

    public static int execute(String s, String s1, Hashtable hashtable)
        throws Exception
    {
        return update(s, fixParams(s1, hashtable));
    }

    public static int execute(String s, Hashtable hashtable)
        throws Exception
    {
        return update("DEFAULT", fixParams(s, hashtable));
    }

    public static int execute(Connection connection, String s)
        throws Exception
    {
        return update(connection, s);
    }

    public static int execute(Connection connection, String s, Hashtable hashtable)
        throws Exception
    {
        return update(connection, fixParams(s, hashtable));
    }

    private static String fixParams(String s, Hashtable hashtable)
        throws Exception
    {
        StringBuffer stringbuffer = new StringBuffer();
        String s1;
        for(StringTokenizer stringtokenizer = new StringTokenizer(s, "#$", true); stringtokenizer.hasMoreTokens(); stringbuffer.append(s1))
        {
            s1 = stringtokenizer.nextToken();
            if("#".equals(s1))
            {
                s1 = (String)hashtable.get(stringtokenizer.nextToken());
                if(s1 == null)
                    s1 = "0";
                stringtokenizer.nextToken();
            }
            if("$".equals(s1))
            {
                s1 = (String)hashtable.get(stringtokenizer.nextToken());
                if(s1 == null)
                    s1 = "NULL";
                s1 = "'" + Functions.fixQuote(s1) + "'";
                stringtokenizer.nextToken();
            }
        }

        return stringbuffer.toString();
    }

    private static Result retreive(String s, String s1)
        throws Exception
    {
        Connection connection = ConnectionPool.getConnection(s);
        Statement statement = null;
        Result result = null;
        try
        {
            try
            {
                statement = connection.createStatement();
            }
            catch(Exception _ex)
            {
//                ConnectionPool.recreate(s, connection);
                statement = connection.createStatement();
            }
            if(DEBUG)
                System.out.println("DBUtil Debug executeQuery: " + s1);
            result = storeValues(statement.executeQuery(s1));
        }
        finally
        {
            if(statement != null)
                statement.close();
            ConnectionPool.release(s, connection);
        }
        return result;
    }

    private static Result retreive(Connection connection, String s)
        throws Exception
    {
        Statement statement = null;
        Result result = null;
        try
        {
            statement = connection.createStatement();
            if(DEBUG)
                System.out.println("DBUtil Debug executeQuery: " + s);
            result = storeValues(statement.executeQuery(s));
        }
        finally
        {
            if(statement != null)
                statement.close();
        }
        return result;
    }

    public static Result select(String s)
        throws Exception
    {
        return retreive("DEFAULT", s);
    }

    public static Result select(String s, String s1)
        throws Exception
    {
        return retreive(s, s1);
    }

    public static Result select(String s, String s1, Hashtable hashtable)
        throws Exception
    {
        return retreive(s, fixParams(s1, hashtable));
    }

    public static Result select(String s, Hashtable hashtable)
        throws Exception
    {
        return retreive("DEFAULT", fixParams(s, hashtable));
    }

    public static Result select(Connection connection, String s)
        throws Exception
    {
        return retreive(connection, s);
    }

    public static Result select(Connection connection, String s, Hashtable hashtable)
        throws Exception
    {
        return retreive(connection, fixParams(s, hashtable));
    }

    private static Result storeValues(ResultSet resultset)
        throws Exception
    {
        Result result = null;
        try
        {
            ResultSetMetaData resultsetmetadata = resultset.getMetaData();
            int i = resultsetmetadata.getColumnCount();
            String as[] = new String[i];
            for(int j = 0; j < i; j++)
                as[j] = resultsetmetadata.getColumnName(j + 1);

            Vector vector = new Vector();
            String as1[];
            for(; resultset.next(); vector.addElement(as1))
            {
                as1 = new String[i];
                for(int l = 0; l < i; l++)
                    as1[l] = resultset.getString(l + 1);

            }

            int k = vector.size();
            result = new Result(k, i);
            for(int i1 = 0; i1 < k; i1++)
                result.values[i1] = (String[])vector.elementAt(i1);

            result.columnNames = as;
            resultset.close();
        }
        finally
        {
        }
		return result;
    }

    private static int update(String s, String s1)
        throws Exception
    {
        Connection connection = ConnectionPool.getConnection(s);
        Statement statement = null;
        int i = -1;
        try
        {
            try
            {
                statement = connection.createStatement();
            }
            catch(Exception _ex)
            {
//                ConnectionPool.recreate(s, connection);
                statement = connection.createStatement();
            }
            if(DEBUG)
                System.out.println("DBUtil Debug executeUpdate: " + s1);
            i = statement.executeUpdate(s1);
        }
        finally
        {
            if(statement != null)
                statement.close();
            ConnectionPool.release(s, connection);
        }
        return i;
    }

    private static int update(Connection connection, String s)
        throws Exception
    {
        Statement statement = null;
        int i = -1;
        try
        {
            statement = connection.createStatement();
            if(DEBUG)
                System.out.println("DBUtil Debug executeUpdate: " + s);
            i = statement.executeUpdate(s);
        }
        finally
        {
            if(statement != null)
                statement.close();
        }
        return i;
    }

    public static boolean DEBUG = false;

}
